package cn.com.utt.common.protorpc.codec;



import java.io.DataInput;
import java.io.IOException;

import com.google.common.base.Preconditions;

import io.netty.buffer.ByteBuf;


public final class Varint {

  private Varint() {
  }


  public static int putSignedVarLong(long value, byte[] buf) throws IOException {
    // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types
    return putUnsignedVarLong((value << 1) ^ (value >> 63), buf);
  }


  public static int putUnsignedVarLong(long value,byte[] buf) throws IOException {
	  System.out.println("value:"+value);
	int i=0;
    while (value>=0x80) {
    	buf[i]= (byte)(value|0x80);
    	value >>>= 7;
    	i++;
    }
    buf[i] = (byte)value;
    return i+1;
  }

  /**
   * @see #writeSignedVarLong(long, java.io.ByteBuf)
   */
  public static void writeSignedVarInt(int value, ByteBuf out) throws IOException {
    // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types
    writeUnsignedVarInt((value << 1) ^ (value >> 31), out);
  }


  public static void writeUnsignedVarInt(int value, ByteBuf out) throws IOException {
    while ((value & 0xFFFFFF80) != 0L) {
      out.writeByte((value & 0x7F) | 0x80);
      value >>>= 7;
    }
    out.writeByte(value & 0x7F);
  }

  public static long readSignedVarLong(ByteBuf in) throws IOException {
    long raw = readUnsignedVarLong(in);
    // This undoes the trick in writeSignedVarLong()
    long temp = (((raw << 63) >> 63) ^ raw) >> 1;
    // This extra step lets us deal with the largest signed values by treating
    // negative results from read unsigned methods as like unsigned values
    // Must re-flip the top bit if the original read value had it set.
    return temp ^ (raw & (1L << 63));
  }

  
  public static long readUnsignedVarLong(ByteBuf in) throws IOException {
    long value = 0L;
    int i = 0;
    long b;
    while (((b = in.readByte()) & 0x80L) != 0) {
      System.out.println("readUnsignedVarlong byte:"+b);
      value |= (b & 0x7F) << i;
      i += 7;
      Preconditions.checkArgument(i <= 63, "Variable length quantity is too long (must be <= 63)");
    }
    return value | (b << i);
  }

  
  public static int readSignedVarInt(DataInput in) throws IOException {
    int raw = readUnsignedVarInt(in);
    // This undoes the trick in writeSignedVarInt()
    int temp = (((raw << 31) >> 31) ^ raw) >> 1;
    // This extra step lets us deal with the largest signed values by treating
    // negative results from read unsigned methods as like unsigned values.
    // Must re-flip the top bit if the original read value had it set.
    return temp ^ (raw & (1 << 31));
  }

 
  public static int readUnsignedVarInt(DataInput in) throws IOException {
    int value = 0;
    int i = 0;
    int b;
    while (((b = in.readByte()) & 0x80) != 0) {
      value |= (b & 0x7F) << i;
      i += 7;
      Preconditions.checkArgument(i <= 35, "Variable length quantity is too long (must be <= 35)");
    }
    return value | (b << i);
  }

}